package com.partsoft.dits.dsm;

import com.partsoft.dits.EdgeDescriptor;

public class DataSourceDescriptor implements EdgeDescriptor, Cloneable {
	
    private String driverClassName;
    
    private String username;

    private String password;

    private String url;
    
    private int maxActive = 50;
    
    private long maxWait= -1;
    
    private int initialSize=1;
  
    private String name;
    
    private String monitorState;
    
    private boolean configurable = false;
    
    private String title;
    
    private String description;
    
    private String validationQuery;
    
    private int ordinal = 0;
    
    public DataSourceDescriptor() {}
    
    public DataSourceDescriptor(String name){
    	setName(name);
    }
    
    public DataSourceDescriptor(String name, String driverClassName, String url,
			String username, String password) {
    	setName(name);
    	this.driverClassName = driverClassName;
		this.url = url;
		this.username = username;
		this.password = password;
    }
    
	public DataSourceDescriptor(String name, String driverClassName, String url,
			String username, String password, int maxActive, int initialSize,
			int maxWait) {
		setName(name);
		this.driverClassName = driverClassName;
		this.url = url;
		this.username = username;
		this.password = password;
		this.maxActive = maxActive;
		this.initialSize = initialSize;
		this.maxWait = maxWait;
	}
	
	public String getValidationQuery() {
		return validationQuery;
	}

	public void setValidationQuery(String validationQuery) {
		this.validationQuery = validationQuery;
	}
	
	public int getOrdinal() {
		return ordinal;
	}

	public void setOrdinal(int ordinal) {
		this.ordinal = ordinal;
	}

	public String getDriverClassName() {
		return driverClassName;
	}
	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public int getMaxActive() {
		return maxActive;
	}
	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}
	public long getMaxWait() {
		return maxWait;
	}
	public void setMaxWait(long maxWait) {
		this.maxWait = maxWait;
	}
	public int getInitialSize() {
		return initialSize;
	}
	public void setInitialSize(int initialSize) {
		this.initialSize = initialSize;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name == null ? name : name.trim();
	}

	public String getMonitorState() {
		return monitorState;
	}

	public void setMonitorState(String monitorState) {
		this.monitorState = monitorState;
	}
	
	public boolean isConfigurable() {
		return configurable;
	}

	public void setConfigurable(boolean configurable) {
		this.configurable = configurable;
	}
	
	public String getTitle() {
		return title == null ? getName() : title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description == null ? "" : description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public DataSourceDescriptor clone() {
		try {
			return (DataSourceDescriptor) super.clone();
		} catch (Throwable e) {
			return null;
		}
	}
	
	public String getId() {
		return getName();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DataSourceDescriptor other = (DataSourceDescriptor) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return String.format("DataSource(%s)[url=%s]", name, url);
	}
	
}